import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'EMPImageUtils.dart';
import 'EMPThemeUtils.dart';
import 'EMPButton.dart';
import 'EMPLayoutUtils.dart';
import 'EMPColorUtils.dart';

/// 自定义AppBar
class EMPAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Color? backgroundColor;
  final String title;
  final String centerTitle;
  final String backImg;
  final Color? backImgColor;
  final String actionName;
  final VoidCallback? onPressed;
  final bool? isBack;

  const EMPAppBar(
      {this.backgroundColor,
      this.title = '',
      this.centerTitle = '',
      this.actionName = '',
      this.backImg = 'other/ic_back_black',
      this.backImgColor,
      this.onPressed,
      this.isBack = true});

  @override
  Widget build(BuildContext context) {
    final Color bgColor = backgroundColor ?? context.backgroundColor;

    // final SystemUiOverlayStyle overlayStyle = (ThemeData.estimateBrightnessForColor(bgColor) == EMPTheme.light) as SystemUiOverlayStyle;

    final Widget action = actionName.isNotEmpty
        ? Positioned(
            right: 0.0,
            child: Theme(
                data: Theme.of(context).copyWith(
                  buttonTheme: const ButtonThemeData(
                    padding: EdgeInsets.symmetric(horizontal: 16.0),
                    minWidth: 60.0,
                  ),
                ),
                child: TextButton(
                  onPressed: onPressed,
                  style: ButtonStyle(
                    overlayColor: MaterialStateProperty.resolveWith<Color?>((states) {
                      return Colors.transparent; // 设置水波纹颜色为透明，不显示水波纹效果
                    }),
                  ),
                  child: Text(
                    textAlign: TextAlign.left, // 设置文字居左
                    softWrap: true, // 开启自动换行 默认开启
                    actionName,
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.normal,
                      color: EMPColor.text_black,
                      overflow: TextOverflow.visible,
                    ),
                  ),
                )
            ),
            // EMPButton(
            //   key: const Key('actionName'),
            //   fontSize: Dimens.font_sp14,
            //   minWidth: null,
            //   text: actionName,
            //   textColor: context.isDark ? EMPColor.dark_text : EMPColor.text,
            //   // backgroundColor: Colors.transparent,
            //   onPressed: onPressed,
            // ),
          )
        : EMPLayout.empty;

    final Widget back = isBack == true
        ? IconButton(
            onPressed: () async {
              FocusManager.instance.primaryFocus?.unfocus();
              final isBack = await Navigator.maybePop(context);
              if (!isBack) {
                await SystemNavigator.pop();
              }
            },
            tooltip: 'Back',
            padding: const EdgeInsets.all(12.0),
            icon: Image(
              image: EMPImageUtils.getAssetImage('other/ic_back_black'),
              width: 15,
              color: backImgColor ?? EMPTheme.getIconColor(context),
            ),
          )
        : EMPLayout.empty;

    final Widget titleWidget = Semantics(
      namesRoute: true,
      header: true,
      child: Container(
        alignment: centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
        width: double.infinity,
        margin: const EdgeInsets.symmetric(horizontal: 48.0),
        child: Text(
          title.isEmpty ? centerTitle : title,
          style: const TextStyle(
            fontSize: Dimens.font_sp18,
          ),
        ),
      ),
    );

    return Material(
      // color: bgColor,
      child: SafeArea(
        child: Stack(
          alignment: Alignment.centerLeft,
          children: <Widget>[
            titleWidget,
            back,
            action,
          ],
        ),
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(48.0);
}
